package com.qinyeit.serviceapp.service.thirdpart;

import com.google.common.collect.Lists;
import com.qinyeit.serviceapp.exception.BusinessRunTimeException;
import com.qinyetech.springstage.core.http.okhttp.HttpUtils;
import com.qinyetech.springstage.core.lang.QyMap;
import okhttp3.ResponseBody;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.List;

@Service
@Transactional
public class PlatformApiServiceImpl {

    private Log log = LogFactory.getLog(getClass());

    @Value("${platformGateway:}")
    private String platformGateway = "";

    //验证注册码是否可用
    public QyMap verifyRegKey(String activeKey) {
        try{
            String url = platformGateway+"/api/regCode/verifyRegKey?platform=catering&code="+activeKey;
            ResponseBody body = HttpUtils.post(url)
                    .execute()
                    .body();
            if(body==null){
                throw new BusinessRunTimeException("注册码验证失败", 1001);
            }
            String result = body.string();
            log.info(">>>>>>>>>>>>>>"+result);
            QyMap map = QyMap.WRAP(result);
            if(map.getInt("status")!=200){
                throw new BusinessRunTimeException(map.getString("message"), 1001);
            }
            return map.getAs("content", QyMap.class);
        }catch (Exception e){
            e.printStackTrace();
            if(e instanceof BusinessRunTimeException){
                throw (BusinessRunTimeException)e;
            }
            throw new BusinessRunTimeException("注册码验证失败", 1001);
        }
    }

    //更新验证码
    public void updateRegKey(String activeKey) {
        try{
            String url = platformGateway+"/api/regCode/updateRegKey?platform=catering&code="+activeKey;
            ResponseBody body = HttpUtils.post(url)
                    .execute()
                    .body();
            if(body==null){
                throw new BusinessRunTimeException("注册码验证失败", 1001);
            }
            String result = body.string();
            log.info(">>>>>>>>>>>>>>"+result);
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    //查询菜系
    public List<QyMap> queryCuisines() {
        try{
            String url = platformGateway+"/api/queryCuisine";
            ResponseBody body = HttpUtils.get(url)
                    .execute()
                    .body();
            if(body==null){
                throw new BusinessRunTimeException("查询菜系失败", 1001);
            }
            String result = body.string();
            log.info(">>>>>>>>>>>>>>"+result);
            QyMap map = QyMap.WRAP(result);
            if(map.getInt("status")!=200){
                throw new BusinessRunTimeException(map.getString("message"), 1001);
            }
            return map.getList("content", QyMap.class);
        }catch (Exception e){
            e.printStackTrace();
        }
        return Lists.newArrayList();
    }


    //查询行业
    public List<QyMap> queryIndustries() {
        try{
            String url = platformGateway+"/api/queryIndustry";
            ResponseBody body = HttpUtils.get(url)
                    .execute()
                    .body();
            if(body==null){
                throw new BusinessRunTimeException("查询行业失败", 1001);
            }
            String result = body.string();
            log.info(">>>>>>>>>>>>>>"+result);
            QyMap map = QyMap.WRAP(result);
            if(map.getInt("status")!=200){
                throw new BusinessRunTimeException(map.getString("message"), 1001);
            }
            return map.getList("content", QyMap.class);
        }catch (Exception e){
            e.printStackTrace();
        }
        return Lists.newArrayList();
    }

}
